<?php

namespace App\Console\Stats\Platform\Settlement\Job;

use App\Console\Stats\Command;
use App\Models\Admin\StatsTask\Platform\Settlement\Month as Task;
use App\Jobs\Stats\Platform\Settlement\Month as Job;

class Month extends Command
{

    /**
     * 控制台命令 signature 的名称。
     *
     * @var string
     */
    protected $signature = 'stats:platform:settlement:job:month';

    /**
     * 控制台命令说明。
     *
     * @var string
     */
    protected $description = '【平台收益】发布任务到队列【月】';

    /**
     * 执行控制台命令。
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            $this->info($this->description);

            $this->info('发布到队列中...');
            while (true) {
                if (!count($tasks = Task::getTasks())) {
                    break;
                }
                $tasks->each(function ($task) {
                    $task->status = Task::STATUS_JOB;
                    $task->save();

                    Job::dispatch($task);
                });
            }
            $this->info('发布到队列完成');

            $this->info('执行成功。');
        } catch (\Throwable $e) {
            $this->error($e->getMessage());
            $this->error('执行失败。');
            throw $e;
        }
    }
}
